mui.init();
var baseUrl = '192.168.3.68:8080';
var loginBtn = document.getElementById('btn_login');
var userAccount = document.getElementById('inpt_mobile');
var userPwd = document.getElementById('inpt_pwd');
var md5pwd = md5(userPwd.value || "");
var backTime = 0;
var rememberStatus;

mui.plusReady(function() {

	if (localStorage.getItem('rememberStatus') === 'false') {
		localStorage.setItem("rememberStatus",'false');
		rememberStatus = false;
		document.getElementById('autoLogin').classList.remove('mui-active');
	} else {
		rememberStatus = localStorage.getItem('rememberStatus');
		document.getElementById('autoLogin').classList.add('mui-active');
	}
	var localName = localStorage.getItem('rememberName');
	var localPwd = localStorage.getItem('rememberPwd');
	if (rememberStatus && localName && localPwd) {
		userAccount.value = localName;
		userPwd.value = localPwd;
	}
	mui.back = function(){
		if(backTime == 0){
			backTime = new Date().getTime();
			mui.toast('再按一次返回键退出应用');
			setTimeout(function(){
				backTime = 0;
			},1000);
		}else {
			if (new Date().getTime() - backTime < 1000){
				plus.runtime.quit();
			}
		}
	}
	setTimeout(function(){
		// close除了当前webview以外的所有webview
		var all = plus.webview.all(); 
	//	console.log(JSON.stringify(all));
		var current = plus.webview.currentWebview().id; 
		for(var i=0,len=all.length;i<len;i++){
		    if(all[i].id!==current && all[i].id!== plus.runtime.appid){ 
		        all[i].close(); 
		    } 
		}
	}, 200);
	document.getElementById('autoLogin').addEventListener('toggle', function(event) {
		//event.detail.isActive 可直接获取当前状态
		if (event.detail.isActive) {
			rememberStatus = true;
		}else {
			rememberStatus = false;
		}
	});
	loginBtn.addEventListener('tap', function(e) {
		var _this = mui(this);
		if (userAccount.value == '') {
			mui.toast('请输入用户名')
			return
		}
		if (userPwd.value == '') {
			mui.toast('请输入密码')
			return
		}
	//	alert(userAccount.value + md5(userPwd.value || ""))
		_this.button('loading');
		
		request("/api/login", "POST",{
//			account: 'xf102',
//			password: md5('123456')
			account: userAccount.value,
			password: md5(userPwd.value || "")
		}, function(res) {
			console.log(JSON.stringify(res))
			if(res.code == 0) {
				var roleType = res.result.roleType;
				var unitId = res.result.unitId;
				var userId = res.result.id;
				if (rememberStatus) {
					localStorage.setItem("rememberName",userAccount.value);
					localStorage.setItem("rememberPwd",userPwd.value);
					localStorage.setItem("rememberStatus",'true');
				} else {
					localStorage.removeItem('rememberName');
					localStorage.removeItem('rememberPwd');
					localStorage.setItem("rememberStatus",'false');
				}
				localStorage.setItem("roleType",roleType);
				localStorage.setItem("unitId",unitId);
				localStorage.setItem("userId",userId);
				var userInfo = JSON.stringify(res.result);
				localStorage.setItem("userInfo",userInfo);
				_this.button('reset');
				mui.toast('登录成功');
				window.location.href = '../main.html';
			}
			else{
				_this.button('reset');
				mui.toast(res.desc);
			}
		}, true, function() {
			_this.button('reset');
			mui.toast('服务器异常');
		});
	
	
  })
})
